package org.example.lys.stream;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liyishan
 * @date 2023/6/21 14:09
 * @apiNote  统计
 */

public class test03 {

    public static void main(String[] args) {
        List<User> userList = build();
        test9001(userList);
    }
    /**
     * 对列表中的的对象的某个属性进行统计
     * @param userList  List<User> userList
     * return   count
     */
    private static void test7(List<User> userList){
        long count = userList.stream().filter(user -> user.getName().equals("一")).count();
        System.out.println(count);
    }

    /**
     * 把对象列表的某个属性进行add
     * @param userList
     */
    private  static void test8(List<User> userList){
        BigDecimal result = userList.stream().map(User::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println(result);
    }

    private static void test80001(List<User> userList){
        Integer count= userList.stream().map(User::getId).reduce((a,b) -> a+b).get();
        System.out.println(count);
    }

    private static void test80002(List<User> userList){
        Integer count = userList.stream().mapToInt(User::getId).sum();
        System.out.println(count);
    }

    private static void test80003(List<User> userList){
        BigDecimal maxPrice= userList.stream().max(Comparator.comparing(User::getPrice)).get().getPrice();
        System.out.println(maxPrice);
    }


    private static void test80004(List<User> userList){
        Integer count = userList.stream().mapToInt(User::getId).min().getAsInt();
        System.out.println(count);
    }

    private static void test80005(List<User> userList){
        Optional<User> count = userList.stream().min(Comparator.comparing(User::getId));
        System.out.println(count);
    }


    private static void test80006(List<User> userList){
        Optional<User> max = userList.stream().max(Comparator.comparing(User::getId));
        if (max.isPresent()){
            System.out.println("max = " + max);
        }
    }

    private static void test80007(List<User> userList){
        // 求某个字段的平均值
        Double collect2 = userList.stream().collect(Collectors.averagingInt(User::getId));
        System.out.println("collect2 = " + collect2);

        // 简化后
        OptionalDouble average = userList.stream().mapToDouble(User::getId).average();
        if (average.isPresent()){
            System.out.println("average = " + average);
        }
    }

    private static void test9001(List<User> userList){
        // 拼接某个字段的值,用逗号分隔，并设置前缀和后缀
        String collect3 = userList.stream().map(User::getName).collect(Collectors.joining(",", "111", "222"));
        System.out.println("collect3 = " + collect3);
    }
    private static List<User> build(){
        List<User> userList = new ArrayList<>();
        User user1= new User();
        user1.setId(1);
        user1.setName("大一小");
        user1.setPrice(new BigDecimal("1.11"));
        userList.add(user1);
        User user2= new User();
        user2.setId(2);
        user2.setName("大二小");
        user2.setPrice(new BigDecimal("2.22"));
        userList.add(user2);
        User user3= new User();
        user3.setId(3);
        user3.setName("大三小");
        user3.setPrice(new BigDecimal("3.33"));
        userList.add(user3);
        User user31= new User();
        user31.setId(4);
        user31.setName("一大三二小");
        user31.setPrice(new BigDecimal("3.33"));
        userList.add(user31);
        return userList;
    }

}
